﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Xml.Serialization;
using System.IO;
using ConToList.dd;

namespace ConToList
{
    class Program
    {
        static void Main(string[] args)
        {
            Cat c = new Cat();
            {
               
                c.Title = "右文//aa";
                c.list = new List<Ff>() { };
                c.list.Add(new Ff() { List = new List<string>(), Id=4444 });
                c.b = true;
                c.Dt = DateTime.Now;
                c.ff = new Ff();
                c.strArray = new string[] { "dd","在喷加工中"};
                string xmlResult = ObjectToXML<Cat>(c);
            }
            {

                CatDto c2 = new CatDto();
                c2.Title = "右文//aa";
                c2.list = new List<FfDto>() { };
                c2.list.Add(new FfDto() { List = new List<string>() });
                c2.b = true;
                c2.Dt = DateTime.Now;
                string xmlResult2 = ObjectToXML<CatDto>(c2);
            }

           // Console.WriteLine(xmlResult);

            //var s= XMLToObject<Cat>(xmlResult);

            CatDto dto = Mapper<Cat, CatDto>(c);
            Console.Read();
        }

        public static T2 Mapper<T1, T2>(T1 t1)
            where T1 : class
            where T2 : class
        {
            string xml = ObjectToXML<T1>(t1, typeof(T2).Name);//, 
            return XMLToObject<T2>(xml);
        }

        /// <summary>
        /// 反序列化XML字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="xml"></param>
        /// <returns></returns>
        public static T XMLToObject<T>(string xml) where T : class
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
            xmlSerializer.UnknownNode += new XmlNodeEventHandler(xmlSerializer_UnknownNode);
            StringBuilder sb = new StringBuilder();
            using (StringWriter sw = new System.IO.StringWriter(sb))
            {
                object o = xmlSerializer.Deserialize(new StringReader(xml));
                return o as T;
            }
        }

        /// <summary>
        /// 序列化对象
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string ObjectToXML<T>(T t, string rootName = null)
        {
            XmlSerializer xmlSerializer = string.IsNullOrEmpty(rootName)
                ? new XmlSerializer(typeof(T))
                : new XmlSerializer(typeof(T), new XmlRootAttribute(rootName));
            xmlSerializer.UnknownNode += new XmlNodeEventHandler(xmlSerializer_UnknownNode);
            xmlSerializer.UnknownAttribute +=new XmlAttributeEventHandler(xmlSerializer_UnknownAttribute);
            StringBuilder sb = new StringBuilder();
            XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();
            xsn.Add(string.Empty, string.Empty);
            Encoding defaultEncoding = Encoding.UTF8;
            using (MemoryStream stream = new MemoryStream())
            {
                using (StreamWriter sw = new System.IO.StreamWriter(stream))
                {
                    xmlSerializer.Serialize(sw, t, xsn);
                    int count = (int)stream.Length;
                    byte[] byteArr = new byte[count];
                    stream.Seek(0, SeekOrigin.Begin);
                    stream.Read(byteArr, 0, count);
                    sw.Flush();
                    sw.Close();
                    string xmlResult = defaultEncoding.GetString(byteArr).Trim();
                    return xmlResult;
                }
            }
        }

        static void xmlSerializer_UnknownAttribute(object sender, XmlAttributeEventArgs e)
        {
        }

        static void xmlSerializer_UnknownNode(object sender, XmlNodeEventArgs e)
        {
            //var s= ;
        }

        ///// <summary>
        ///// 序列化对象为XML字符串(new)
        ///// </summary>
        ///// <param name="instance">需要序列化的对象</param>
        ///// <returns>对象序列化之后生成的XML字符串</returns>
        //public static string WriteXmlObject<T>(T instance, IEnumerable<Type> knownTypes = null)
        //{
        //    using (var ms = new MemoryStream())
        //    {
        //        var serializer = new DataContractSerializer(typeof(T), knownTypes);
        //        serializer.WriteObject(ms, instance);
        //        ms.Seek(0, SeekOrigin.Begin);
        //        using (var reader = new StreamReader(ms, Encoding.UTF8))
        //        {
        //            return reader.ReadToEnd();
        //        }
        //    }
        //}
    }

    namespace dd
    {
        public class Cat
        {
            public int Id;
            public string Title;
            public DateTime Dt;
            public bool? b;
           public Ff ff;
            public List<Ff> list;
            public int? iiiiiiiiiiiiiii;
            public string[] strArray;
        }

        public class Ff {
            public int? Id;
            public List<string> List;
        }

           //  [XmlRoot("Cat")]
        public class CatDto
        {
            public string[] strArray;
            public int Id;
            public string Title;
            public DateTime Dt;
            public bool? b;
            [XmlElement("ff")]
            public FfDto ff;

            [XmlArrayItem("Ff")]
            public List<FfDto> list;

            public CatDto()
            {
                
            }
        }
        //[XmlRoot("Ff")]
        public class FfDto
        {
            public int? Id;
            public List<string> List;
        }
    }
}
